package com.zr.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.service.AddMoreUserService;
import com.zr.service.impl.AddMoreUserServiceImpl;

import net.sf.json.JSONObject;

/**
 * 处理批量导入请求
 * @author lzq
 *
 */
public class AddMoreUserAction extends HttpServlet{
	private AddMoreUserService addMoreUserService = new AddMoreUserServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		String filePath = req.getParameter("filePath");
		int f = filePath.indexOf('.');
		String filePathSuffix = filePath.substring(f, filePath.length());
		PrintWriter pw = resp.getWriter();
		JSONObject json = new JSONObject();
		if(".xls".equals(filePathSuffix) || ".xlsx".equals(filePathSuffix)){
			File file = new File(filePath);
			String msg = addMoreUserService.getAddMoreUserByExcel(file);
			json.put("success", true);
			json.put("msg", msg);
		}else{
			json.put("success", false);
			json.put("msg", "上传文件格式错误!");
		}
		pw.write(json.toString());
		pw.flush();
	}
}
